package com.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.db.common.pojo.PageObject;
import com.db.dao.SysLogDao;
import com.db.pojo.SysLog;
import com.db.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import java.util.Arrays;

@Service
public class SysLogServiceImpl implements SysLogService {

	@Autowired
	private SysLogDao sysLogDao;

	@Transactional(readOnly = true)
	@Override
	public PageObject<SysLog> findObjects(String username, Long pageCurrent) {
		QueryWrapper<SysLog> wrapper = new QueryWrapper<>();
		if (!StringUtils.isEmptyOrWhitespace(username)) {
			wrapper.like("username", username);
		} else {
			wrapper = null;
		}
		IPage<SysLog> page = new Page<>(pageCurrent, 7);
		IPage<SysLog> iPage = sysLogDao.selectPage(page, wrapper);
		return new PageObject<>(pageCurrent, 7, iPage.getTotal(), iPage.getPages(), iPage.getRecords());
	}

	@Override
	public void deleteById(Long[] id) {
		sysLogDao.deleteBatchIds(Arrays.asList(id));
	}

	@Transactional(propagation = Propagation.REQUIRES_NEW)
	@Override
	public void insertObject(SysLog sysLog) {
		sysLogDao.insert(sysLog);
	}
}
